package controller

import (
	device_grpc "device-admin/internal/app/grpc/service/device"
	"device-admin/internal/app/grpc/service/terminal"

	"192.168.1.75/go-pkg/errorx"
	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type Sale struct{}

// SetDoorStatus 远程控制电子锁
func (Sale) SetDoorStatus(c *gin.Context) {
	var params struct {
		RobotID  string `json:"robot_id"`
		DoorType int    `json:"door_type"`
		Status   int    `json:"status"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	err := device_grpc.DoorCtlCMDPack(params.RobotID, params.DoorType, params.Status)
	response.JSON(c, nil, err)
}

// SetLiftPosition 设置升降盘位置
func (Sale) SetLiftPosition(c *gin.Context) {
	var params struct {
		RobotID string `json:"robot_id"`
		Floor   int    `json:"floor"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	err := device_grpc.LiftPostionCMDPack(params.RobotID, params.Floor)
	response.JSON(c, nil, err)
}

// GetSaleStatus 获取售卖状态
func (Sale) GetSaleStatus(c *gin.Context) {
	seq, status, err := device_grpc.SaleStatusCMDPack(c.Query("robot_id"))
	response.JSON(c, map[string]interface{}{
		"seq":    seq,
		"status": status,
	}, err)
}

// StopSale 停止售卖
func (Sale) StopSale(c *gin.Context) {
	var params struct {
		RobotID string `json:"robot_id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	seq, err := device_grpc.StopSaleCMDPack(params.RobotID)
	response.JSON(c, map[string]interface{}{
		"seq": seq,
	}, err)
}

// ResumeSale 回复售卖
func (Sale) ResumeSale(c *gin.Context) {
	var params struct {
		RobotID string `json:"robot_id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	seq, err := device_grpc.ResumeSaleCMDPack(params.RobotID)
	response.JSON(c, map[string]interface{}{
		"seq": seq,
	}, err)
}

// Reboot 重启97
func (Sale) Reboot(c *gin.Context) {
	var params struct {
		RobotID string `json:"robot_id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	if params.RobotID == "" {
		response.JSON(c, nil, errorx.New("设备id不能为空", -1))
		return
	}
	terminal.SendShellCMDTo95(params.RobotID, []string{
		"adb disconnect",           // 断开所有的adb连接
		"adb connect 192.168.0.97", // 连接97
		"adb reboot",               // 重启97
	}, 10)
	response.JSON(c, nil, nil)
}
